package com.test.clyq.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.test.clyq.entity.QuestionCategory;
import com.test.clyq.entity.Users;
import com.test.clyq.service.QuestionCategoryService;
import com.test.json.JsonUtil;
import com.test.util.FormatExceptionStack;
import com.test.util.ObjectUtils;
import com.test.util.SessionUtil;
import com.test.util.StringUtils;
import com.test.webapp.action.BaseAction;

/**
 * QuestionCategoryAction. <br>
 * 问题类别管理.
 * <p>
 * Copyright: Copyright (c) 2010-4-17 下午04:52:10
 * <p>
 * Company: 北京宽连十方数字技术有限公司
 * <p>
 * Author: suncg@c-platform.com
 * <p>
 * Version: 1.0
 * <p>
 */
public class QuestionCategoryAction extends BaseAction {

	/**
	 * 添加一个问题类别
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	public ActionForward addQuestionCategory(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
	        throws IOException {
		this.log.info("添加一个问题类别表start");
		QuestionCategoryService questionService = (QuestionCategoryService) getBean("questionCategoryService");
		Users user = SessionUtil.getUserFromSession(request);
		try {
			if (!ObjectUtils.isNull(user)) {
				QuestionCategory question = new QuestionCategory();
				question.setName(getParameter(request, "name"));
				question.setCorder(0);
				// question.setWebsite(user.getUsersWebsite());
				questionService.addQuestionCategory(question, user);
				print(response, true);
				this.log.debug("添加一个问题类别成功");
				this.log.info("添加一个问题类别end");
			} else {
				print(response, false);
			}
		}
		catch (Exception e) {
			this.log.error("添加一个问题类别:失败 " + FormatExceptionStack.formatException(e));
			print(response, false);
		}
		return null;
	}

	/**
	 * 删除问题类别
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	public ActionForward deleteQuestionCategory(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
	        throws IOException {
		this.log.info("删除问题类别start");
		QuestionCategoryService questionService = (QuestionCategoryService) getBean("questionCategoryService");
		Users user = SessionUtil.getUserFromSession(request);
		try {
			if (!ObjectUtils.isNull(user)) {
				String questionId = getParameter(request, "questionCategoryId");
				if (StringUtils.isNumeric(questionId)) {
					questionService.deleteQuestionCategory(Long.valueOf(questionId), user);
					print(response, true);
					this.log.debug("删除问题类别成功");
					this.log.info("删除问题类别end");
				}
			} else {
				print(response, false);
			}
		}
		catch (Exception e) {
			this.log.error("删除问题类别:失败 " + FormatExceptionStack.formatException(e));
			print(response, false);
		}
		return null;

	}

	/**
	 * 获得一个问题类别
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	public ActionForward getQuestionCategory(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
	        throws IOException {
		this.log.info("获得一个问题类别表start");
		QuestionCategoryService questionService = (QuestionCategoryService) getBean("questionCategoryService");
		Users user = SessionUtil.getUserFromSession(request);
		String questionId = getParameter(request, "questionCategoryId");
		try {
			if (!ObjectUtils.isNull(user) && StringUtils.isNumeric(questionId)) {
				QuestionCategory question = questionService.getOneQuestionCategory(Long.valueOf(questionId));
				if (!ObjectUtils.isNull(question)) {
					this.log.debug("获得一个问题类别成功");
					this.log.info("获得一个问题类别end");
					print(response, "{results:[{%s}]}", null, null, question.toJSON());
				} else {
					print(response, false);
				}

			} else {
				print(response, false);
			}

		}
		catch (Exception e) {
			this.log.error("获得一个问题类别:失败 " + FormatExceptionStack.formatException(e));
			print(response, false);
		}
		return null;
	}

	/**
	 * 获得问题类别列表
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	public ActionForward getQuestionCategorys(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
	        throws IOException {
		this.log.info("获得问题类别列表start");
		QuestionCategoryService questionService = (QuestionCategoryService) getBean("questionCategoryService");
		try {
			List<QuestionCategory> list = questionService.getQuestionCategorys();
			String jsonStr = JsonUtil.getJsonStr(list, new String[] { "id", "name", "order" }, 100);
			this.log.debug("获得问题类别列表成功");
			this.log.info("获得问题类别列表end");
			print(response, jsonStr, null, null);
		}
		catch (Exception e) {
			this.log.error("获得问题类别:失败 " + FormatExceptionStack.formatException(e));
			print(response, false);
		}
		return null;
	}

	/**
	 * 获得调查类别列表
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return 栏目列表
	 * @throws IOException
	 */
	public ActionForward getQuestionTree(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
	        throws IOException {
		this.log.info(" 获得调查类别列表start");
		Users user = SessionUtil.getUserFromSession(request);
		QuestionCategoryService questionService = (QuestionCategoryService) getBean("questionCategoryService");
		try {
			List<QuestionCategory> list = questionService.getQuestionCategorys(user.getUsersWebsite());
			String str = com.test.json.JsonTree.getJsonNoCheckStringByList(list, "name", "level");
			this.log.debug(" 获得调查类别列表成功");
			this.log.info(" 获得调查类别列表end");
			print(response, str, null, null);
		}
		catch (Exception e) {
			this.log.error(" 获得调查类别列表:失败 " + FormatExceptionStack.formatException(e));
			print(response, false);
		}
		return null;
	}

	/**
	 * 修改一个问题类别
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	public ActionForward updateQuestionCategory(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
	        throws IOException {
		this.log.info("修改一个问题类别表start");
		QuestionCategoryService questionService = (QuestionCategoryService) getBean("questionCategoryService");
		Users user = SessionUtil.getUserFromSession(request);
		String questionId = getParameter(request, "id");
		try {
			if (!ObjectUtils.isNull(user) && StringUtils.isNumeric(questionId)) {
				QuestionCategory question = questionService.getOneQuestionCategory(Long.valueOf(questionId));
				question.setName(getParameter(request, "name"));
				question.setCorder(Integer.valueOf(getParameter(request, "corder")));
				questionService.updateQuestionCategory(question, user);
				print(response, true);
				this.log.debug("修改一个问题类别成功");
				this.log.info("修改一个问题类别end");
			} else {
				print(response, false);
			}
		}
		catch (Exception e) {
			this.log.error("修改一个问题类别:失败 " + FormatExceptionStack.formatException(e));
			print(response, false);
		}
		return null;
	}
}
